package org.example.demo.apiv2.sql;


import cn.hutool.core.util.StrUtil;
import lombok.Data;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectItem;
import org.example.demo.apiv2.entity.CustomColumnEntity;

import java.util.ArrayList;
import java.util.List;

@Data
public class CustomColumnParser {

    /**
     * 构造jsqlparser的SelectItem对象
     *
     * @param customColumnEntityList
     * @return
     */
    public static List<SelectItem<?>> getSelectItemList(List<CustomColumnEntity> customColumnEntityList) {
        String simpleSqlTemplate = "select {} FROM table";
        StringBuilder selectItemListString = new StringBuilder();

        for (int i = 0; i < customColumnEntityList.size(); i++) {
            CustomColumnEntity customColumnEntity = customColumnEntityList.get(i);
            String columnStr = customColumnEntity.getExpressionStr();
            if (StrUtil.isNotEmpty(customColumnEntity.getName())) {
                columnStr = columnStr + " AS " + customColumnEntity.getName();
            }
            if (i != customColumnEntityList.size() - 1) {
                columnStr = columnStr + ", ";
            }
            selectItemListString.append(columnStr);
        }

        String finalSql = simpleSqlTemplate.replace("{}", selectItemListString);
        Statement statement = null;
        try {
            statement = CCJSqlParserUtil.parse(finalSql);
        } catch (JSQLParserException e) {
            throw new RuntimeException(e);
        }
        Select selectStatement = (Select) statement;
        return selectStatement.getPlainSelect().getSelectItems();
    }

    public static void main(String[] args) throws JSQLParserException {
        String sql = "select greatest(coalesce(a.column1, a_1.column2)) AS result, column1 AS col1, column2 FROM user";
        Statement statement = CCJSqlParserUtil.parse(sql);
        Select selectStatement = (Select) statement;
        List<SelectItem<?>> selectItems = selectStatement.getPlainSelect().getSelectItems();
        System.out.println(selectItems);

        CustomColumnEntity customColumnEntity1 = new CustomColumnEntity();
        customColumnEntity1.setExpressionStr("greatest(coalesce(a.column1, a_1.column2))");
        customColumnEntity1.setName("result");

        CustomColumnEntity customColumnEntity2 = new CustomColumnEntity();
        customColumnEntity2.setExpressionStr("column1");
        customColumnEntity2.setName("col1");

        CustomColumnEntity customColumnEntity3 = new CustomColumnEntity();
        customColumnEntity3.setExpressionStr("column2");

        ArrayList<CustomColumnEntity> columnEntityList = new ArrayList<>();
        columnEntityList.add(customColumnEntity1);
        columnEntityList.add(customColumnEntity2);
        columnEntityList.add(customColumnEntity3);

        List<SelectItem<?>> selectItemList = getSelectItemList(columnEntityList);
        System.out.println(selectStatement);
    }
}
